package io.github.mymonstercat.utils;

import com.benjaminwan.ocrlibrary.Point;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.mymonstercat.entity.CenterPoint;
import io.github.mymonstercat.entity.LocationProperties;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;

@Component
public class SpatialSimilarityCompare {

    @Value("${hwnlp.location-api-url}")
    private String locationUrl;

    public String requestPyApi(double[][] A, double[][] B) throws JsonProcessingException, UnsupportedEncodingException {

        // 创建请求数据
        Map<String, double[][]> requestData = new HashMap<>();
        requestData.put("A", A);
        requestData.put("B", B);

        // 将请求数据转换为 JSON 字符串
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String json = objectMapper.writeValueAsString(requestData);
            System.out.println(json);
            // 创建 HTTP 客户端
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(locationUrl);
            httpPost.setHeader("Content-type", "application/json");
            httpPost.setEntity(new StringEntity(json));

            // 发送请求并获取响应
            CloseableHttpResponse response = httpClient.execute(httpPost);
            String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
            System.out.println(responseString);
            response.close();
            return responseString;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    // 将 ArrayList<CenterPoint> 转换为 double[][]
    public static double[][] toDoubleArray(ArrayList<CenterPoint> centerPoints) {
        double[][] result = new double[centerPoints.size()][2];

        for (int i = 0; i < centerPoints.size(); i++) {
            CenterPoint point = centerPoints.get(i);
            result[i][0] = point.getX();
            result[i][1] = point.getY();
        }

        return result;
    }

    public static double[][] convertListToArray(List<List<Double>> list) {
        // 获取列表的大小
        int rows = list.size();
        int cols = list.get(0).size();

        // 创建二维数组
        double[][] array = new double[rows][cols];

        // 将 List<List<Double>> 转换为 double[][]
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                array[i][j] = list.get(i).get(j);
            }
        }
        return array;
    }

    // 取中心点
    public static CenterPoint getCenterPoint(ArrayList<Point> points){

        CenterPoint centerPoint = new CenterPoint();

        double centerX = 0;
        double centerY = 0;

        for (Point point : points) {
            centerX += point.getX();
            centerY += point.getY();
        }

        centerX /= points.size();
        centerY /= points.size();

        centerPoint.setX(centerX);
        centerPoint.setY(centerY);

        return centerPoint;
    }

    public static List<Double> calculateCenter(List<List<Double>> coordinates) {
        // 假设坐标列表中包含四个点：[左上角, 右上角, 右下角, 左下角]
        double x1 = coordinates.get(0).get(0);  // 左上角 x
        double y1 = coordinates.get(0).get(1);  // 左上角 y
        double x2 = coordinates.get(2).get(0);  // 右下角 x
        double y2 = coordinates.get(2).get(1);  // 右下角 y

        // 计算中心点
        double centerX = (x1 + x2) / 2;
        double centerY = (y1 + y2) / 2;

        return Arrays.asList(centerX, centerY);
    }

    public static List<Double> findCoordinates(List<LocationProperties.OrgPoint> points, String searchText) {
        for (LocationProperties.OrgPoint point : points) {
            if (point.getText().equals(searchText)) {
                return point.getCoordinates();
            }
        }
        return null; // Return null if not found
    }


//    public static void main(String[] args) throws UnsupportedEncodingException, JsonProcessingException {
//        ArrayList<CenterPoint> centerPoints = new ArrayList<>();
//        centerPoints.add(new CenterPoint(1.0, 2.0));
//        centerPoints.add(new CenterPoint(3.0, 4.0));
//
//        double[][] doubleBoxPointLists = SpatialSimilarityCompare.toDoubleArray(centerPoints);
//
//        SpatialSimilarityCompare.requestPyApi(doubleBoxPointLists, doubleBoxPointLists);
//    }
}
